package com.xiyou.week01.stack;

import java.util.Stack;

/**
 * 155. 最小栈
 * <p>
 * 输入：
 * ["MinStack","push","push","push","getMin","pop","top","getMin"]
 * [[],[-2],[0],[-3],[],[],[],[]]
 * <p>
 * 输出：
 * [null,null,null,null,-3,null,0,-2]
 * <p>
 * 解释：
 * MinStack minStack = new MinStack();
 * minStack.push(-2);
 * minStack.push(0);
 * minStack.push(-3);
 * minStack.getMin();   --> 返回 -3.
 * minStack.pop();
 * minStack.top();      --> 返回 0.
 * minStack.getMin();   --> 返回 -2.
 *
 * @ClassName：LeetCode155
 * @Author：西柚
 * @Date：2022/1/4 3:38 下午
 * @Versiion：1.0
 */
public class MinStack {

    Stack<Integer> s;
    Stack<Integer> preMin;

    public MinStack() {
        s = new Stack<Integer>();
        preMin = new Stack<Integer>();
    }

    public void push(int val) {
        s.push(val);
        if (preMin.empty() || val <= preMin.peek()) {
            preMin.push(val);
        } else {
            preMin.push(preMin.peek());
        }
    }

    public void pop() {
        s.pop();
        preMin.pop();
    }

    public int top() {
        return s.peek();
    }

    public int getMin() {
        return preMin.peek();
    }
}

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(val);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.getMin();
 */